mod model;
mod parse;

#[cfg(not(target_arch = "wasm32"))]
pub fn parse_sql(sql: &str, dialect: &str) -> Result<Vec<model::TableInfo>, String> {
    parse::parse_sql(sql.to_owned(), dialect)
}

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn parse_sql(sql: &str, dialect: Option<String>) -> Result<String, String> {
    if sql.trim().is_empty() {
        return Ok("null".to_string());
    }

    let dialect = dialect.unwrap_or_else(|| "_".to_string());

    match parse::parse_sql(sql.to_string(), &dialect) {
        Ok(tables) => {
            if tables.is_empty() {
                Ok("[]".to_string()) // 返回空数组
            } else {
                serde_json::to_string(&tables).map_err(|e| format!("Serialization error: {}", e))
            }
        }
        Err(e) => Err(format!("Parse error: {}", e)),
    }
}
